<?php

class celldata extends Module {
	var $module_name = "celldata";
	var $module_version = 0.1;
	var $module_description = "Provide cellular data from YouLoc database";

	function __construct() {
		parent::__construct();
	}

	function run() {
		try {
			$this->unique_cells();

		} catch (Exception $e) {
			Header($_SERVER["SERVER_PROTOCOL"]." 500 Internal Server Error");
			echo 'Caught exception: ',  $e->getMessage(), "\n";
			exit;
	
		}
	}

	function unique_cells() {
		global $dbh;
        
		$sql = "select cell as cellid, lac, mnc, mcc, avg(lat) as lat, avg(lon) as lon, count(*) as finds from gps_cell_data where lat != 0 and lon != 0 group by cell, mcc, mnc, lac;";
		$stmt = $dbh->prepare($sql);
		$stmt->execute();
		$rows = $stmt->fetchAll();


header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
Header("Content-type: application/vnd.google-earth.kml+xml");

print '<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
        <name>Collected Cellular tower data</name>
';

		foreach ($rows as $rs) {
		printf('<Placemark>
<name>%d</name>
<description><![CDATA[
    CellID: %d<br />
    LAC: %d<br />
    MMC: %d<br />
    MNC: %d<br />
    Amount of submitted coordinates: %d<br />
    ]]></description>
<Point>
  <coordinates>%s,%s,0</coordinates>
</Point>
</Placemark>', 
	$rs["cellid"], 
	$rs["cellid"],
	$rs["lac"],
	$rs["mcc"],
	$rs["mnc"],
	$rs["finds"],
	$rs["lon"],
	$rs["lat"]
);
		}

print '</Document>
</kml>';

	}

	

}

?>
